package com.faiz.ugla.griplog;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.os.Environment;
import android.util.Log;

public class DataWriter {

	private static final String DEBUG_TAG = "DataWriter";

	//private File root;
	private File logFile;
	private FileWriter logWriter = null;
	private BufferedWriter bufferedWriter;
	private String directoryName = "/HandGrip";
	private File workingDir;
	private File appDir;

	public DataWriter() {
		appDir = new File(Environment.getExternalStorageDirectory(), directoryName);

		if(!appDir.exists()) {
			Log.d(DEBUG_TAG, "App directory does not exist");
			if(appDir.mkdir()) {
				Log.d(DEBUG_TAG, "App directory has been created");
			} else {
				Log.d(DEBUG_TAG, "Problem in creating app directory");
			}
		}
	}

	public void createDir(String dirName) {
		String path = "/P" + dirName;

		workingDir = new File(appDir + path);
		Log.d(DEBUG_TAG, "Path: " + path);

		if(!workingDir.exists()) {
			Log.d(DEBUG_TAG, "P" + dirName + " doesn't exist");
			if(workingDir.mkdir()) {
				Log.d(DEBUG_TAG, dirName + " has been created.");
			}else
				Log.d(DEBUG_TAG, "Problem in creating directory " + workingDir);
		} else {
			Log.d(DEBUG_TAG, dirName + " exists");
		}
	}

	public void createFile(String task) {
		//createDir(folderName);
		String fileName = "T" + task + ".csv";

		logFile = new File(workingDir, fileName);

		try {
			logWriter = new FileWriter(logFile);
			bufferedWriter = new BufferedWriter(logWriter);
		} catch (IOException e) {
			Log.e(DEBUG_TAG, "Can't create " + fileName);
		}
	}

	public void writeMetaData(String startTime, String participantID, String accelType, String taskNumber) {
		try {
			bufferedWriter.write("Capture start: " + startTime);
			bufferedWriter.newLine();
			bufferedWriter.write("Participant ID: " + participantID);
			bufferedWriter.newLine();
			bufferedWriter.write("Task: " + taskNumber);
			bufferedWriter.newLine();
		} catch (IOException e) {
			Log.e(DEBUG_TAG, "Can't write file");
		} 
	}

	public void writeData(long t, int e, int[] acc, int[] gyro, int[] cap) {
		try {
			Log.d(DEBUG_TAG, "writing to file..." + e);
			bufferedWriter.write(t + "," + e + "," + acc[0] + "," + acc[1] + "," + acc[2] + "," + gyro[0] + "," + gyro[1] + "," + gyro[2] + "," + cap[0] + ", " + cap[1] + "," + cap[2] + ", " + cap[3] + "," + cap[4] + ", " + cap[5] + "," + cap[6] + ", " + cap[7] + "," + cap[8] + ", " + cap[9] + "," + cap[10] + ", " + cap[11] + "," + cap[12] + ", " + cap[13] + "," + cap[14] + ", " + cap[15] + "," + cap[16] + ", " + cap[17] + "," + cap[18] + ", " + cap[19] + "," + cap[20] + ", " + cap[21] + "," + cap[22] + ", " + cap[23]);
			bufferedWriter.newLine();
		} catch (IOException ioe) {
			Log.e(DEBUG_TAG, "Can't write file");
		} 
	}

	public void closeFile() {
		try {
			bufferedWriter.flush();
			bufferedWriter.close();
			Log.d(DEBUG_TAG, "File closed...");
		} catch (IOException e) {
			Log.e(DEBUG_TAG, "Can't close file");
		}
	}
}
